using System.Collections;
using UnityEngine;

namespace RGuang.PlathformCtrl
{
    public class StarGem : MonoBehaviour
    {
        [SerializeField] float resetDelayTime = 3f;
        [SerializeField] AudioClip pickUpSFX;
        [SerializeField] ParticleSystem pickUpVFX;

        MeshRenderer meshRenderer;
        new Collider collider;
        WaitForSeconds waitForReset;
        private void Awake()
        {
            collider = GetComponent<Collider>();
            meshRenderer = GetComponentInChildren<MeshRenderer>();
            waitForReset = new WaitForSeconds(resetDelayTime);
        }

        private void OnTriggerEnter(Collider other)
        {
            if (other.TryGetComponent<PlayerController>(out var player))
            {
                player.CanAirJump = true;
                meshRenderer.enabled = false;
                collider.enabled = false;
                SoundEffectPlayer.AudioSource.PlayOneShot(pickUpSFX);
                Instantiate(pickUpVFX, transform.position, transform.rotation);
                StartCoroutine(nameof(ResetCoroutine));
            }
        }

        private void Reset()
        {
            collider.enabled = true;
            meshRenderer.enabled = true;
        }


        IEnumerator ResetCoroutine()
        {
            yield return waitForReset;
            Reset();
        }
    }

}
